package com.canbot.u05.activity.shoppingGuide.sales;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.canbot.u05.R;
import com.canbot.u05.activity.BaseActivityNoTitle;
import com.canbot.u05.socket.MsgSendUtils;
import com.canbot.u05.socket.MsgType;
import com.canbot.u05.utils.Logger;
import com.canbot.u05.utils.ToastTools;
import com.canbot.u05.view.RouteXFListenerDialog;

/**
 * 导购模式文字转语音界面
 */
public class TextToSpeechActivity extends BaseActivityNoTitle implements View.OnClickListener {

        private EditText editText;

        private TextView btnSure;

        private ImageView btnVoice;

        private String fileName;

        private String voice;

        private RouteXFListenerDialog usherModeRecordDialog;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_text_to_speech);
                setViews();
                voice = getIntent().getStringExtra("voice");
                int type = getIntent().getIntExtra("type", -1);
                if (voice != null) {
                        editText.setText(voice);
                }

        }

        private void setViews() {
                editText = (EditText) findViewById(R.id.shopping_mode_editText_text_to_speech);
                btnVoice = (ImageView) findViewById(R.id.iv_shopping_mode_text_to_speech_voice_icon);
                btnSure = (TextView) findViewById(R.id.btn_complete);
                btnSure.setOnClickListener(this);
                btnVoice.setOnClickListener(this);
                editText.addTextChangedListener(watcher);
                editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                        @Override
                        public void onFocusChange(View v, boolean hasFocus) {
                                editText.setSelection(editText.length());
                        }
                });
        }

        private TextWatcher watcher = new TextWatcher() {

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                        if (s.length() > 500) {
                                editText.setText(s.subSequence(0, 500));
                                editText.setSelection(500);
                                ToastTools.newInstance(TextToSpeechActivity.this).showText(getString(R.string.input_up_words2));

                        }
                }

                @Override
                public void beforeTextChanged(CharSequence s, int start, int count,
                                              int after) {

                }

                @Override
                public void afterTextChanged(Editable s) {

                }
        };

        @Override
        public void onClick(View view) {
                switch (view.getId()) {
                        case R.id.btn_complete:
                                //确定
                                String trim = editText.getText().toString().trim();
                                Logger.d("TextToSpeechActivity", trim);
                                if (TextUtils.isEmpty(trim)) {
                                        ToastTools.newInstance(this).showText(getString(R.string.please_enter_content));
                                        return;
                                }
                                Intent intent = new Intent();
                                intent.putExtra("isChecked", true);
                                intent.putExtra("ttsResult", trim);
                                setResult(RESULT_OK, intent);
                                finish();
                                break;

                        case R.id.iv_shopping_mode_text_to_speech_voice_icon:
                                String s = editText.getText().toString();
                                if (!TextUtils.isEmpty(s)) {
                                        //播放语音
                                        boolean isSuccess = MsgSendUtils.sendStringMsg(MsgType.PLAY_TTS, s);
                                        if (!isSuccess) {
                                                ToastTools.newInstance(this).showText(getString(R.string.network_error));
                                        }
                                }
                                break;
//
                }
        }


        public void back(View view) {
                MsgSendUtils.sendStringMsg(MsgType.STOP_SOUND, "stop");
                //取消
                finish();
                overridePendingTransition(R.anim.face_zoom_enter,
                        R.anim.face_zoom_exit);
        }

        RouteXFListenerDialog.IXunFeiResult xunFeiResult = new RouteXFListenerDialog.IXunFeiResult() {
                @Override
                public void onResult(String result) {

                        editText.append(result);
                }

                @Override
                public void onError(int result) {
                        ToastTools.newInstance(TextToSpeechActivity.this).showText(getString(R.string.identification_error) + result);
                }
        };

        public static void setDialogWindowAttr(Dialog dlg, Context ctx) {
                Window window = dlg.getWindow();
                WindowManager.LayoutParams lp = window.getAttributes();
                lp.gravity = Gravity.CENTER;
                lp.width = (int) ctx.getResources().getDimension(R.dimen.x536);//宽高可设置具体大小
                lp.height = (int) ctx.getResources().getDimension(R.dimen.x436);
                dlg.getWindow().setAttributes(lp);
        }
}
